package top.xlz999.tyme;

import anywheresoftware.b4a.AbsObjectWrapper;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.objects.collections.List;
import com.tyme.eightchar.EightChar;
import com.tyme.sixtycycle.SixtyCycle;
import com.tyme.solar.SolarTerm;
import com.tyme.solar.SolarTime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.function.Function;
import java.util.stream.Collectors;

@BA.ShortName("tyme4j")
@BA.Version(1.12f)
@BA.Author("Icefairy333")
@BA.DependsOn(values = {"tyme4j-1.1.2.jar"})
public class tyme4j {
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_FORMAT);


    public tyme4j() {
        this.sdf.setTimeZone(TimeZone.getTimeZone(DATETIME_FORMAT));
    }

    /**
     * 根据年月日时分秒获取农历纪年
     *
     * @param y  年 1-9999
     * @param m  月
     * @param d  日
     * @param h  时
     * @param mi 分
     * @param si 秒
     * @return 甲辰 癸酉 己卯 庚午
     */
    public String LunarFromYMDH(int y, int m, int d, int h, int mi, int si) {
        return new SolarTime(y, m, d, h, mi, si).getLunarHour().toString();
    }

    /**
     * 根据年月日时分秒获取干支纪年
     *
     * @param y  年 1-9999
     * @param m  月
     * @param d  日
     * @param h  时
     * @param mi 分
     * @param si 秒
     * @return 甲辰 癸酉 己卯 庚午
     */
    public String GanzhiFromYMDH(int y, int m, int d, int h, int mi, int si) {
        return new SolarTime(y, m, d, h, mi, si).getLunarHour().getEightChar().toString();
    }

    /**
     * 根据年月日时分秒获取四柱旬空
     *
     * @param y  年 1-9999
     * @param m  月
     * @param d  日
     * @param h  时
     * @param mi 分
     * @param si 秒
     * @return 寅卯 戌亥 申酉 戌亥
     */
    public String XunkongFromYMDH(int y, int m, int d, int h, int mi, int si) {
        return Arrays.stream(new SolarTime(y, m, d, h, mi, si).getLunarHour().getEightChar().toString().split(" ")).map(s -> GanzhiToXunkong(s)).collect(Collectors.joining(" "));
    }

    /**
     * 根据六十甲子获取旬空
     *
     * @param jiazi 甲辰
     * @return 寅卯
     */
    public String GanzhiToXunkong(String jiazi) {
        return Arrays.stream(SixtyCycle.fromName(jiazi).getExtraEarthBranches()).map(earthBranch -> earthBranch.getName()).collect(Collectors.joining(""));
    }

    /**
     * 干支反推日期
     *
     * @param ganzhi     甲辰 癸酉 己卯 庚午
     * @param startYear
     * @param endYear
     * @param ignoreHour
     * @return [2024-09-12 12:00:00, 2204-09-29 12:00:00, 2264-09-14 12:00:00, 2444-09-30 12:00:00, 2504-09-16 12:00:00, 2684-10-02 12:00:00, 2744-09-18 12:00:00, 2924-10-04 12:00:00, 2984-09-19 12:00:00]
     */
    public List GanzhiToDate(String ganzhi, int startYear, int endYear, boolean ignoreHour) {
        anywheresoftware.b4a.objects.collections.List r = new anywheresoftware.b4a.objects.collections.List();
        r.Initialize();
        String[] gz = ganzhi.trim().split(" ");
        if (gz.length != 4) {
            return r;
        }
        EightChar ec = new EightChar(gz[0], gz[1], gz[2], gz[3]);
        ec.getSolarTimes(startYear, endYear, ignoreHour).forEach(solarTime -> {
            Calendar c = Calendar.getInstance();
            c.set(solarTime.getYear(), solarTime.getMonth() - 1, solarTime.getDay(), solarTime.getHour(), solarTime.getMinute(), solarTime.getSecond());
            r.Add(sdf.format(c.getTime()));
        });
        return r;
    }

    /**
     * 时间戳到日期时间
     *
     * @param tick
     * @return yyyy-MM-dd HH:mm:ss
     */
    public String TickToDateTime(long tick) {
        Date d = new Date(tick);
        return sdf.format(d);
    }

    /**
     * 日期时间到时间戳
     *
     * @param dt yyyy-MM-dd HH:mm:ss
     * @return
     */
    public long DatetimeToTick(String dt) {
        try {
            return sdf.parse(dt).getTime();
        } catch (ParseException e) {
            return 0;
        }
    }

    /**
     * 日期时间获取节气
     *
     * @param y
     * @param m
     * @param d
     * @param h
     * @param mi
     * @param si
     * @return
     */
    public Jieqi JieqiFromYMDH(int y, int m, int d, int h, int mi, int si) {
        SolarTime st = new SolarTime(y, m, d, h, mi, si);
        SolarTerm st2 = st.getTerm();
        return new Jieqi().init2(y, st2.getIndex());
    }

    /**
     * 根据干支获取索引
     *
     * @param jz
     * @return
     */
    public int GanzhiToIndex(String jz) {
        return SixtyCycle.fromName(jz).getIndex();
    }

    /**
     * 根据索引获取干支
     *
     * @param index
     * @return
     */
    public String GanzhiFromIndex(int index) {
        return SixtyCycle.fromIndex(index).getName();
    }

    /**
     * 干支获取纳音
     *
     * @param index
     * @return
     */
    public String GanzhiToNayin(int index) {
        return SixtyCycle.fromIndex(index).getSound().getName();
    }

    /**
     * 干支获取纳音2
     *
     * @param name
     * @return
     */
    public String GanzhiToNayin2(String name) {
        return SixtyCycle.fromName(name).getSound().getName();
    }

    /**
     * 数组转字符串
     * @param arr
     * @param sep
     * @return
     */
    public String ArrayToString(Object[] arr,String sep){
        return Arrays.stream(arr).map((Function<Object, String>) s -> s.toString()).collect(Collectors.joining(sep));
    }

    /**
     * 字符串转数组
     * @param s1
     * @param sep
     * @return
     */
    public Object[] StringToArray(String s1, String sep){
        return Arrays.stream(s1.split(sep)).collect(Collectors.toList()).toArray();
    }

    @BA.ShortName("Jieqi")
    public static class Jieqi extends AbsObjectWrapper<SolarTerm> {
        public Jieqi init(long t) {
            Date d = new Date();
            d.setTime(t);
            SolarTime st = new SolarTime(d.getYear(), d.getMonth(), d.getDay(), d.getHours(), d.getMinutes(), d.getSeconds());
            setObject(st.getTerm());
            return this;
        }

        public Jieqi init2(int year, int index) {
            setObject(new SolarTerm(year, index));
            return this;
        }

        public Jieqi init3(int year, String name) {
            setObject(new SolarTerm(year, name));
            return this;
        }

        /**
         * 获取节气名称
         *
         * @return
         */
        public String getName() {
            return getObject().getName();
        }

        /**
         * 获取节气索引
         *
         * @return
         */
        public int getIndex() {
            return getObject().getIndex();
        }

        /**
         * 获取下一个节气
         *
         * @return
         */
        public Jieqi getNextJieqi() {
            Jieqi jq = new Jieqi();
            int y = getObject().getJulianDay().getSolarTime().getYear();
            return jq.init2(y, getObject().getIndex() + 1);
        }

        /**
         * 获取上一个节气
         *
         * @return
         */
        public Jieqi getPreJieqi() {
            Jieqi jq = new Jieqi();
            int y = getObject().getJulianDay().getSolarTime().getYear();
            return jq.init2(y, getObject().getIndex() - 1);
        }

        @Override
        public String toString() {
            Calendar c = Calendar.getInstance();
            SolarTime st = getObject().getJulianDay().getSolarTime();
            c.set(st.getYear(), st.getMonth(), st.getDay(), st.getHour(), st.getMinute(), st.getSecond());
            return getObject().getName() + " " + tyme4j.sdf.format(c.getTime());
        }
    }

    //
    public static void main(String[] args) {

        tyme4j t = new tyme4j();
        Jieqi jq = t.JieqiFromYMDH(2024, 9, 12, 1, 1, 1);
        System.out.println(jq);
        System.out.println(jq.getNextJieqi());
        System.out.println(jq.getPreJieqi());
//        long t1=System.currentTimeMillis();
//        String ts=t.TickToDateTime(t1);
//        t1=t.DatetimeToTick(ts);
//        System.out.println(ts);
//        System.out.println(t1);
    }
}
